package com.ize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ize.entity.dto.AdminLoginDto;
import com.ize.entity.dto.DoctorInfoUpdateDto;
import com.ize.entity.dto.UpdatePaasDto;
import com.ize.entity.pojo.Doctor;
import com.ize.entity.vo.AdminLoginVo;
import com.ize.mapper.DoctorMapper;
import com.ize.properties.JwtClaimsConstant;
import com.ize.properties.JwtProperties;
import com.ize.service.DoctorService;
import com.ize.util.BaseContext;
import com.ize.util.JwtUtil;
import com.ize.util.MD5util;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class doctorServiceImpl extends ServiceImpl<DoctorMapper,Doctor> implements DoctorService {

    private final DoctorMapper doctorMapper;
    private final JwtProperties jwtProperties;

    /**
     * 医生注册
     * @param adminLoginDto
     */
    @Override
    public void register(AdminLoginDto adminLoginDto) {
        String password = adminLoginDto.getPassword();
//        String realPass = adminLoginDto.getReal_pass();
//        if (!password.equals(realPass)) {
//            throw new RuntimeException("原密码和确认密码不符");
//        }
        String new_pass = MD5util.getMD5String(password);
        Doctor doctor = new Doctor();
        doctor.setPassword(new_pass);
        doctor.setUsername(adminLoginDto.getUsername());
//        doctorMapper.register(doctor);
        doctorMapper.insert(doctor);
    }

    /**
     * 医生登录
     * @param adminLoginDto
     * @return
     */
    @Override
    public AdminLoginVo login(AdminLoginDto adminLoginDto) {
        //1.获取密码
        String password = adminLoginDto.getPassword();
        //2.加密
        String s = MD5util.getMD5String(password);
        //查数据库
        //Doctor doctor = doctorMapper.getDocterByUsername(adminLoginDto.getUsername());
        LambdaQueryWrapper<Doctor> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.eq(/*Doctor对象中的变量指向数据库字段*/Doctor::getUsername, adminLoginDto.getUsername());
        Doctor doctor = doctorMapper.selectOne(lambdaWrapper);
        //判断是否为空
        if (doctor == null) {throw new RuntimeException("用户为空");}
        //判断密码是否正确
        if (!s.equals(doctor.getPassword())) {throw new RuntimeException("密码不正确");}
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, doctor.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        System.out.println(token);
        //封装结果
        AdminLoginVo loginVo = new AdminLoginVo().builder()
                .id(doctor.getId())
                .username(doctor.getUsername())
                .token(token)
                .build();
        return loginVo;
    }

    /**
     * 获取医生信息
     * @return
     */
    @Override
    public Doctor getDoctorById() {
        //获取用户信息
        Long currentId = BaseContext.getCurrentId();
        //如果用户信息为空
        if(currentId == null){
            throw new RuntimeException("去登录，用户信息为空");
        }
        Doctor doctor = doctorMapper.selectById(currentId);
        doctor.setPassword("********");
        return doctor;
    }

    /**
     * 根据用户信息修改信息
     * @param doctorInfoUpdateDto
     */
    @Override
    public void updateById(DoctorInfoUpdateDto doctorInfoUpdateDto) {
        Doctor doctor = new Doctor();
        BeanUtils.copyProperties(doctorInfoUpdateDto,doctor);
        doctor.setId(BaseContext.getCurrentId());
        doctorMapper.updateById(doctor);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Doctor doctor = new Doctor().builder()
                .id(BaseContext.getCurrentId())
                .image(avatarUrl)
                .build();
        doctorMapper.updateById(doctor);
    }

    /**
     * 修改密码
     * @param updatePaasDto
     */
    @Override
    public void updatePass(UpdatePaasDto updatePaasDto) {
        String oldPassword = updatePaasDto.getOldPassword();//123456
        String newPass = updatePaasDto.getNew_pass();//1234567
        String restPass = updatePaasDto.getRest_pass();//1234567
        //判断确认密码和新密码
        if(!restPass.equals(newPass)){
            throw new RuntimeException("新密码和确认密码不一致");
        }
        //判断旧密码和新密码
        if(oldPassword.equals(newPass)){
            throw new RuntimeException("新密码和旧密码密码一致");
        }
        //查询旧密
        // 使用 LambdaQueryWrapper 指定查询字段
//        LambdaQueryWrapper<Doctor> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.select(Doctor::getPassword); // 替换为你的字段名
//        queryWrapper.eq(BaseContext.getCurrentId());
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("password").eq("id", BaseContext.getCurrentId());
        Doctor doctor = doctorMapper.selectOne(queryWrapper);
        if(!doctor.getPassword().equals(MD5util.getMD5String(oldPassword))){
            throw new RuntimeException("密码错误");
        }
        doctor.setPassword(MD5util.getMD5String(newPass));
        doctor.setId(BaseContext.getCurrentId());
        doctorMapper.updateById(doctor);
    }
}
